package com.example.daojia.frametest.Retrofit;

import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;

/**
 * Created by zhangbin on 2018/7/25.
 */

public class TSRtrofit {
    private Retrofit mRetrofit;

    private TSRtrofit() {
        mRetrofit = new Retrofit.Builder()
                .baseUrl("http://10.253.104.151:8080/") //接口公用部分
                .client(buildClient()) //添加okhttp client，设置超时、拦截等
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(FastJsonConverterFactory.create())
                .build();
    }

    private static class Holder {
        private static TSRtrofit instance = new TSRtrofit();
    }

    public static TSRtrofit getInstance() {
        return Holder.instance;
    }

    public  <T> T createApi(Class<T> clazz) {
        return mRetrofit.create(clazz);
    }

    private OkHttpClient buildClient() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder()
                .retryOnConnectionFailure(true)
                .connectTimeout(20, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS);
//        builder.addInterceptor(new SYRequestInterceptor()); //拦截器
//        builder.addInterceptor(new SYResponseInterceptor());//拦截器
        return builder.build();
    }
}
